#include <stdio.h>
#include <stdlib.h>

#include "board.h"
#include "user.h"
#include "core.h"
#include "debug.h"
#include "display.h"

int init_board(board_list p_board_list, FILE * filename)
{
    int arr[] = {0};
	int len = 0;
    int mid = 0;

    board_list p_board_list = (board_list)malloc(sizeof(board_node));
    p_board_list = read_file(p_board_list, filename);

    if (p_board_list == NULL)
        DEBUG("p_board_list ERROR!\n");

    while (p_board_list != NULL) {
        arr[len] = p_board_list->score;
        p_board_list = p_board_list->next;
        ++ len;
    }

    board_node * cur = p_board_list;
    len = sizeof(arr) / sizeof(arr[0]);
    
    while(mid < len) {
        board_node * new_board_node = (board_node *)malloc(sizeof(board_node));
        new_board_node->score = arr[mid];
        new_board_node->next = NULL;
        cur->next = new_board_node;
        cur = cur->next;
        mid ++;
    }
    bubble_sort(p_board_list);
    //print_board(p_board_list);

    return 0;
}

/**
 * @brief   绘出排行榜
 * @param   board_list为传入排行榜的结构
 * @return  void
 */
void print_board(board_list p_board_list)
{
	int mid = 0;
	const int ranking[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    if (p_board_list == NULL)
        DEBUG("p_board_list ERROR!\n");

    board_node * cur = p_board_list->next;
    printf(L_CYAN 
            "======= LeaderBoard =======\n\n"
            "RANKING\tNAME\t\tSCORE\n\n" NONE);

    while (cur != NULL && mid < 10) {
		cur->num = ranking[mid];
		printf(L_CYAN "%d\t%s\t\t%d\n" NONE, 
                cur->num, cur->name, cur->score);
        cur = cur->next;
		mid ++;
    }
}

/**
 * @brief   冒泡法排序单链表
 * @param   board_list为传入排行榜的结构
 * @return  int
 */
int bubble_sort(board_list p_board_list)
{
    if( (p_board_list->next == NULL) 
		|| (p_board_list->next->next == NULL)) {
        return -1;
    }

    if (p_board_list == NULL)
        DEBUG("p_board_list ERROR!\n");

    board_node *head, * pre, * cur, *next, * end, * temp;
    head = p_board_list;
    end = NULL;
    /* 从链表头开始将较大值往后沉 */
    while(head->next != end) {
        for(pre = head, cur = pre->next, next = cur->next; 
			next != end; 
			pre = pre->next, cur = cur->next, 
			next = next->next) {
            /* 相邻的节点比较 */
            if(cur->score > next->score) {
                cur->next = next->next;
                pre->next = next;
                next->next = cur;
                temp = next;
                next = cur;
                cur = temp;
            }
        }
        end = cur;
    }

    return 1;
}

int destroy_board(board_list p_board_list)
{   
    board_list mid_1, mid_2;

    if (p_board_list == NULL) {
        DEBUG("destroy_board error!\n");
        return 1;
    }

    mid_1 = p_board_list->next;
    p_board_list->next = NULL;

    while (mid_1 != NULL) {
        mid_2 = mid_1;
        mid_1 = mid_1->next;
        free(mid_2);
    }

    return 0;
}
